/*
 * DMAC1.c
 *
 *  Created on: 01/04/2013
 *      Author: RIchard
 */
#include <msp430.h>
#include "DMAC1.h"

#include "../DIO/DIOC1.h"

static void (*DMA_pfCallback)(void);

void DMA_vStartTransfer( DMA_tstTransfer *stTransfer )
{
   /* store callback function */
   DMA_pfCallback = stTransfer->pfFinishCallback;

   /* trigger selection */
   DMACTL0 = DMA0TSEL_24;
   DMACTL4 = DMARMWDIS;

   /* transfer size */
   DMA0SZ = stTransfer->u16Samples;
   /* clear interrupt flag */
   DMA0CTL &= ~DMAIFG;

   /* set sounce and destination addresses */
   __data16_write_addr((unsigned short) &DMA0SA,(unsigned long) stTransfer->pu16Source);
   __data16_write_addr((unsigned short) &DMA0DA,(unsigned long) stTransfer->pu16Destination);

   /* enable DMA */
   /* enable interrupt */
   /* increment destination */
   /* repeat single transfer */
   DMA0CTL = DMAEN + DMAIE + DMADSTINCR_3 + DMADT_4;
}

#pragma vector=DMA_VECTOR
__interrupt void DMA_vISR(void)
{
   switch(__even_in_range( DMAIV, 0x10 ) )
   {
      case DMAIV_NONE:
         break;
      case DMAIV_DMA0IFG:
         if( DMA_pfCallback != NULL )
         {
            /* run callback */
            DMA_pfCallback();
         }
         /* Stop the DMA */
         DMA0CTL &= ~( DMAEN + DMAIE );

         /* TEST */
         MSP430_FAST_CLEAR_PIN_TEST;

         /* Wake Up */
         _BIC_SR_IRQ(LPM3_bits);
         break;
      case DMAIV_DMA1IFG:
      case DMAIV_DMA2IFG:
      default:
         break;
   }
}
